import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/parts/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化备件出运通知'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件出运通知'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件出运通知详情'
});
const addDesc = formatMessage({
    id: 'api.message.newVehicleSaleOrderDesc',
    defaultMessage: '新增备件出运通知'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改备件出运通知'
});
const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代编号'
});

const getDealerSimpleDesc = formatMessage({
    id: 'api.message.getDealerSimpleDesc',
    defaultMessage: '快捷查询总代编号'
});

const getPartShipmentDesc = formatMessage({
    id: 'api.message.getPartShipmentDesc',
    defaultMessage: '快捷查询备件发运单'
});
const outboundDataDesc = formatMessage({
    id: 'api.message.outboundDataDesc',
    defaultMessage: '出库'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/partShipNotice/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partShipNotices?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partShipNotices/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const addApplyReport = data => request(`${baseUrl}/partShipNotices`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const updateApplyReport = (id, data) => request(`${baseUrl}/partShipNotices/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 查询经销商
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
//根据条件查询总代子公司
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerSimpleDesc));
};
//查询发运单
export const getPartShipment = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partShipments/forpartShipNotice?${queryString}`, null, dto.PartShipment)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartShipmentDesc));
};

//查询发运单详情
export const getPartShipmentDetail = id =>
    request(`${baseUrl}/partShipments/${id}/forpartShipNotice`, null, dto.PartShipmentDetail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartShipmentDesc));
//出库
export const outboundData = (id, data) => request(`${baseUrl}/partShipNotices/${id}/outbound`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, outboundDataDesc));
